import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
import Index from '@/components/Index'
import IndexAdd from '@/components/IndexAdd'
import IndexQuery from '@/components/IndexQuery'
import {getCookie} from '@/lib/cookie'
Vue.use(Router)

var routes = new Router({
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/index',
      name: 'Index',
      component: Index,
      children:[{
        path: 'add',
        name: 'Index-Add',
        component: IndexAdd,
      },{
        path: 'query',
        name: 'Index-Query',
        component: IndexQuery,
      }]
    },
    {
      path: '*',
      name: 'Index',
      component: Index
    },
  ]
})
routes.beforeEach(function (to, from,next) {
  var login = getCookie('username')
  if (login || to.path=='/login'){
    next()
  }else{
    next('/login')
  }
})

export default routes
